﻿
Partial Class WebForm_SelfManageWorkHeader
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not IsPostBack Then
            If clsAuth.GetUser().UserType = My.UserType.Employee OrElse clsAuth.GetUser().UserType = My.UserType.Person OrElse clsAuth.GetUser().VolunteerStatus <> My.VolunteerStatus.VBooked Then
                Response.Redirect("~\Public\OnlyForVolunteer.aspx")
            End If

            Me.InitData()
        End If
    End Sub

    Protected Sub SearchWorkHeader_RowUpdating(ByVal sender As Object, ByVal e As System.EventArgs) Handles SearchWorkHeader1.RowUpdating
        '單筆檢視模式
        Me.TabContainer1.ActiveTab = Me.AddTabPanel
        Me.AddWorkHeader1.DataAccessMode = My.DataAccessMode.Update
        Me.AddWorkHeader1.InitData(Me.SearchWorkHeader1.FindDirtyHeaderID, Me.SearchWorkHeader1.SelectedStatus)
    End Sub

    Protected Sub AddWorkHeader1_Saving(ByVal sender As Object, ByVal e As System.EventArgs) Handles AddWorkHeader1.Saving
        If Me.SaveData = True Then
            '列表檢視模式
            Me.TabContainer1.ActiveTab = Me.SearchTabPanel
            Me.SearchWorkHeader1.BindGV()
            MyObj.ShowMessage(Me.GetLocalResourceObject("SaveData_Success"))
        End If
    End Sub

    Protected Sub lknAdd_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lknAdd.Click
        Me.TabContainer1.ActiveTab = Me.AddTabPanel
        Me.InitData()
    End Sub
End Class

Partial Class WebForm_SelfManageWorkHeader
    Public Sub InitData()
        Me.AddWorkHeader1.DataAccessMode = My.DataAccessMode.Insert
        Me.AddWorkHeader1.InitData()
    End Sub

    Protected Sub SearchWorkHeader_RowDeleting(ByVal sender As Object, ByVal e As System.EventArgs) Handles SearchWorkHeader1.RowDeleting
        Dim dat As New clsData
        Dim sql As New MyStringBuilder
        Using scope As New Transactions.TransactionScope
            sql.Length = 0
            sql.AppendFormat("Delete FindDirtyHeader ")
            sql.AppendFormat(" where FindDirtyHeaderID='{0}' ", Me.SearchWorkHeader1.FindDirtyHeaderID)
            dat.AccData(sql)
            MyObj.WriteLog(clsAuth.GetUserID() & "," & Now.ToString() & "," & MyObj.getIP() & "," & sql.ToString())

            sql.Length = 0
            sql.AppendFormat("Delete FindDirtyItem ")
            sql.AppendFormat(" where FindDirtyHeaderID='{0}' ", Me.SearchWorkHeader1.FindDirtyHeaderID)
            dat.AccData(sql)

            scope.Complete()
        End Using
        Me.SearchWorkHeader1.BindGV()
    End Sub

    Private Function SaveData() As Boolean
        Dim dat As New clsData
        Dim sql As New MyStringBuilder

        Using scope As New Transactions.TransactionScope
            If (Me.AddWorkHeader1.DataAccessMode = My.DataAccessMode.Insert) Then
                sql.Length = 0
                sql.AppendFormat("insert FindDirtyHeader (RegionType,VillageID,Location,Description,Status,ReportDate,PhotoPathID,CreateDate,CreateBy) select ")
                sql.AppendFormat(" '{0}' ", Me.AddWorkHeader1.RegionType)
                sql.AppendFormat(",'{0}' ", Me.AddWorkHeader1.VillageID)
                sql.AppendFormat(",'{0}' ", Me.AddWorkHeader1.Location)
                sql.AppendFormat(",'{0}' ", Me.AddWorkHeader1.Description)
                sql.AppendFormat(",'{0}' ", "1")
                sql.AppendFormat(",'{0:yyyy/MM/dd}' ", Me.AddWorkHeader1.ReportDate)
                sql.AppendFormat(",'{0}' ", Me.AddWorkHeader1.PhotoPathID)
                sql.AppendFormat(",{0} ", "getdate()")
                sql.AppendFormat(",'{0}' ", Me.LoginID)
                dat.AccData(sql)
                Dim id As Integer = MyObj.GetIdentity()


                Dim s1 As String = Me.AddWorkHeader1.DirtyItem(0, 0)
                Dim s2 As String = Me.AddWorkHeader1.DirtyItem(0, 1)
                Dim s3 As String = Me.AddWorkHeader1.DirtyItem(0, 2)
                Dim s4 As String = Me.AddWorkHeader1.DirtyItem(0, 3)


                For i As Integer = 0 To (Me.AddWorkHeader1.DirtyItem.Length / 4) - 1
                    If Me.AddWorkHeader1.DirtyItem(i, 3) <> "" Then
                        sql.Length = 0
                        sql.AppendFormat("insert FindDirtyItem (FindDirtyHeaderID,ItemGroupID,ItemID,Amount,DirtyLevel) select ")
                        sql.AppendFormat(" '{0}' ", id)
                        sql.AppendFormat(" ,'{0}' ", Me.AddWorkHeader1.DirtyItem(i, 0))
                        sql.AppendFormat(" ,'{0}' ", Me.AddWorkHeader1.DirtyItem(i, 1))
                        sql.AppendFormat(" ,'{0}' ", Me.AddWorkHeader1.DirtyItem(i, 2))
                        sql.AppendFormat(" ,'{0}' ", Me.AddWorkHeader1.DirtyItem(i, 3))

                        'sql.AppendFormat(",'{0}' ", Me.AddWorkHeader1.DirtyItem(i, 0))
                        'sql.AppendFormat(",'{0}' ", Me.AddWorkHeader1.DirtyItem(i, 1))
                        dat.AccData(sql)
                    End If
                Next

            ElseIf (Me.AddWorkHeader1.DataAccessMode = My.DataAccessMode.Update) Then
                sql.Length = 0
                sql.AppendFormat("Update FindDirtyHeader set")
                sql.AppendFormat(" RegionType='{0}' ", Me.AddWorkHeader1.RegionType)
                sql.AppendFormat(" ,VillageID='{0}' ", Me.AddWorkHeader1.VillageID)
                sql.AppendFormat(" ,Location='{0}' ", Me.AddWorkHeader1.Location)
                sql.AppendFormat(" ,Description='{0}' ", Me.AddWorkHeader1.Description)
                sql.AppendFormat(" ,ReportDate='{0:yyyy/MM/dd}' ", Me.AddWorkHeader1.ReportDate)
                sql.AppendFormat(" ,PhotoPathID='{0}' ", Me.AddWorkHeader1.PhotoPathID)
                sql.AppendFormat(" ,UpdateDate={0} ", "getdate()")
                sql.AppendFormat(" ,UpdateBy='{0}' ", Me.LoginID)
                sql.AppendFormat(" where FindDirtyHeaderID='{0}' ", Me.SearchWorkHeader1.FindDirtyHeaderID)
                dat.AccData(sql)

                sql.Length = 0
                sql.AppendFormat("Delete FindDirtyItem ")
                sql.AppendFormat(" where FindDirtyHeaderID='{0}' ", Me.SearchWorkHeader1.FindDirtyHeaderID)
                dat.AccData(sql)

                For i As Integer = 0 To (Me.AddWorkHeader1.DirtyItem.Length / 4) - 1
                    If Me.AddWorkHeader1.DirtyItem(i, 0) <> "" Then
                        sql.Length = 0
                        sql.AppendFormat("insert FindDirtyItem (FindDirtyHeaderID,ItemGroupID,ItemID,Amount,DirtyLevel) select ")
                        sql.AppendFormat(" '{0}' ", Me.SearchWorkHeader1.FindDirtyHeaderID)
                        sql.AppendFormat(" ,'{0}' ", Me.AddWorkHeader1.DirtyItem(i, 0))
                        sql.AppendFormat(" ,'{0}' ", Me.AddWorkHeader1.DirtyItem(i, 1))
                        sql.AppendFormat(" ,'{0}' ", Me.AddWorkHeader1.DirtyItem(i, 2))
                        sql.AppendFormat(" ,'{0}' ", Me.AddWorkHeader1.DirtyItem(i, 3))

                        'sql.AppendFormat("insert FindDirtyItem (FindDirtyHeaderID,DirtyItemName,DirtyLevel) select ")
                        'sql.AppendFormat(" '{0}' ", Me.SearchWorkHeader1.FindDirtyHeaderID)
                        'sql.AppendFormat(",'{0}' ", Me.AddWorkHeader1.DirtyItem(i, 0))
                        'sql.AppendFormat(",'{0}' ", Me.AddWorkHeader1.DirtyItem(i, 1))
                        dat.AccData(sql)
                    End If
                Next
            End If
            scope.Complete()
        End Using

        Return True
    End Function

    Private ReadOnly Property LoginID() As String
        Get
            Return Context.User.Identity.Name
        End Get
    End Property

End Class